/*
 * monitoringMere.c
 *
 *  Created on: 17 janv. 2012
 *      Author: cavramescu
 */

#include <stdio.h>
#include <string.h>
// Required by for routine
#include <sys/types.h>
#include <sys/errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>   

#include "communicationCapteurs.h"

int globalVariable = 2;

capteurEtat CreerCapteurEtat(char *idCap,int codeEtat,etat e)
{
	capteurEtat ce;
	ce.codeEtat = codeEtat;
	ce.etatCap = e;
	ce.idCapteur = idCap;
	return ce;
}

int listeAjouteCapEtat(struct listCapEtat *liste, capteurEtat ce)
{
	while(liste->next != NULL)
	{
		liste= liste->next;
	}

	struct listCapEtat *l = malloc(sizeof(struct listCapEtat));
	l->capteur = ce;
	l->next = NULL;
	liste->next = l;

	free(l);

	return OK;
}

int main()
{
   //char* sIdentifier;
   //int    iStackVariable = 20;

	//initialisation de la listeCapteurEtat;
	//listeCapEtat *listeCE = malloc (sizeof(*listeCE));

//	listeAjouteCapEtat(listeCE, "0021CC07" , );

   pid_t pID = fork();
   if (pID == 0)                // child
   {
	   // Code only executed by child process
	   communicationCapteurs();
   }
    else if (pID < 0)            // failed to fork
    {
    	printf("failed to fork");
        exit(1);
        // Throw exception
    }
    else                                   // parent
    {
    	// Code only executed by parent process
    	printf("i am the parent\n");
    	int ch;
    	for(;;)
    	{
			ch = getchar();
			printf("read char is: %c\n", ch);
			if(ch == 'a')
			{
				int  killReturn = kill( pID, SIGKILL);  // Kill child process group

				if( killReturn == ESRCH)      // pid does not exist
				{
				   printf("Group does not exist!\n");
				}
				else if( killReturn == EPERM) // No permission to send signal
				{
				   printf("No permission to send signal!\n");
				}
				else
				{
				   printf("Signal sent. All Ok!\n");
				   return 0;
				}
			}
    	}
    }

    // Code executed by both parent and child.
   /*
    printf(sIdentifier);
    printf(" Global variable: %d ", globalVariable);
    printf(" Stack variable: %d\n", iStackVariable);
*/
    return 0;
}


